package com.mocha.common.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.mocha.entity.Blog;
import com.mocha.validate.LoginValidator;


public class HelloController extends Controller{
	
	public void index(){
		renderJsp("add.jsp");
	}
	@Before(LoginValidator.class)
	public void add(){
		String name = getPara("name");
		String pwd = getPara("password");
		System.out.println("name:"+name);
		renderText("欢迎"+name);
	}
	public void listData(){
		String sql = "select * from blog";
		List<Blog> bglist = Blog.Dao.find(sql);
		renderJson(bglist);
	}
	//分页查询
	public void listDataJson(){
		int currentpage = getParaToInt("currentpage");//当前页
		StringBuffer sb =new StringBuffer();
		sb.append("select * from blog limit ");
		sb.append(currentpage+","+10);
		List<Blog> bglist = Blog.Dao.find(sb.toString());
		setAttr("curr", currentpage);
		renderJson(bglist);
	}
	
	public void list(){
		renderJsp("list.jsp");
	}
	
	public void del(){
		String id = getPara("id");
		Blog.Dao.deleteById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flag", true);
		map.put("msg", "数据删除成功！");
		renderJson(map);
	}
	
	public void find(){
		String id = getPara("id");
		Blog blog =	Blog.Dao.findById(id);
		renderJson(blog);
	}
	
	public void lay(){
		renderJsp("lay.jsp");
	}
	
}
